package m.jclecai.com.fragment.klpk;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import m.jclecai.com.MyApplication;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.klpk.bean.HistoryBean;
import m.jclecai.com.bean.klpk.bean.PokerBean;
import m.jclecai.com.bean.klpk.bean.response.PokerHistoryBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.BaseUtils;
import m.jclecai.com.utils.KlpkUtils;
import m.jclecai.com.utils.http.HttpManager;


/**
 * Created by andy on 2017/6/8.
 * 这里的话，我们是显示历史的Fragment;
 */

public class PokerHistoryFragment extends Fragment {

    private RecyclerView mRecyclerView;

    private List<PokerBean> mPokerBeanList;



    private MyAdapter mMyAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_klpk_poker_history, null);

        mRecyclerView = ((RecyclerView) view.findViewById(R.id.recycler_view));
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        //这里我们要做的一个操作，也是进行一个网络的请求；
        mMyAdapter = new MyAdapter();
        mRecyclerView.setAdapter(mMyAdapter);
        LinearLayoutManager mlinearLayout = new LinearLayoutManager(getActivity()) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        mRecyclerView.setLayoutManager(mlinearLayout);

        requestDataFromNet();
    }

    private void requestDataFromNet() {

        HttpManager.getAsyn(Constant_Url.KLPK_ISSUE_LIST, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(String response) {
                try {
                    Gson gson = new Gson();
                    PokerHistoryBean pokerHistoryBean = gson.fromJson(response, PokerHistoryBean.class);
                    mMyAdapter.addData(pokerHistoryBean.getRecords());
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                    ((BaseActivity) getActivity()).DisPlay("网络异常！");
                }

            }
        });
    }

    class MyAdapter extends RecyclerView.Adapter<ViewHolder> {

        public List<HistoryBean> mHistoryBeanList;


        public MyAdapter() {
            mHistoryBeanList = new ArrayList<>();
        }

        public MyAdapter(List<HistoryBean> historyBeanList) {
            mHistoryBeanList = historyBeanList == null ? new ArrayList<HistoryBean>() : historyBeanList;
        }

        public void addData(List<HistoryBean> mPokerList) {

            mPokerList.remove(mHistoryBeanList);
            int newSize = mPokerList.size();
            int pokerListSize = mHistoryBeanList.size();
            mHistoryBeanList.addAll(mPokerList);
            notifyItemRangeInserted(pokerListSize, newSize);
        }

        public void addBean(HistoryBean pokerBean) {
            int size = mHistoryBeanList.size();
            mHistoryBeanList.add(pokerBean);
            notifyItemRangeInserted(size, 1);
        }

        public void removeTop() {
            //这里的话，我是需要将其最上面的移出，将最下面的实现；
        }


        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            //这里的话
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_period_history,parent,false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {

            //这里的话，我们先按照正常的逻辑来，将数据都显示在条目上面；
            HistoryBean historyBean = mHistoryBeanList.get(position);

            String term = historyBean.getTerm();
            //字符串截取最后两位；
            String currentTerm = term.substring(term.length() - 2);
            //将字符串转换为相应的PokerBean对象,这里的话，牌型也是需要我们自己来判断的；
            String result = historyBean.getResult();
            //这里的话，存在一个问题就是，如果还在开奖中的，返回的数据类型是什么样的？我们先做正确的；
            List<PokerBean> pokerBeanList = KlpkUtils.getPokerBeanList(result);
            String brandType = KlpkUtils.getBrandType(pokerBeanList);

            holder.mTvShape.setText(brandType);
            holder.mTvPeriodHistory.setText(currentTerm+"期");
            //设置牌型；
            for (int i = 0; i < pokerBeanList.size(); i++) {
                PokerBean pokerBean = pokerBeanList.get(i);
                PokerBean.Category category = pokerBean.getCategory();

                String pokerNumber = KlpkUtils.pokerNumer[pokerBean.getNumber()];
                holder.mHistoryPokers[i].setText(pokerNumber);
                holder.mHistoryPokers[i].setBackground(getResources().getDrawable(KlpkUtils.pokerHistoryBgRes[category.ordinal()]));

                //如果是10的话的，paddingRight=1dp
                if("10".equals(pokerNumber)){
                    holder.mHistoryPokers[i].setPadding(0,0, BaseUtils.dip2px(getContext(),1),0);
                }else{
                    holder.mHistoryPokers[i].setPadding(0,0, BaseUtils.dip2px(getContext(),5),0);
                }


                if(category== PokerBean.Category.box||category== PokerBean.Category.hearts){
                    holder.mHistoryPokers[i].setTextColor(getResources().getColor(R.color.klpk_text_red_color));
                }else{
                    holder.mHistoryPokers[i].setTextColor(Color.BLACK);
                }
            }
        }

        @Override
        public int getItemCount() {
            return mHistoryBeanList.size();
        }
    }

    //这里的话，如果可以的话，我们可以对其进行一个抽取；
    class ViewHolder extends RecyclerView.ViewHolder {

        private TextView[] mHistoryPokers=new TextView[3] ;
        private TextView mTvPeriodHistory;
        private TextView mTvShape;

        public ViewHolder(View itemView) {

            super(itemView);

            mTvPeriodHistory = (TextView) itemView.findViewById(R.id.tv_period_text);

            mHistoryPokers[0] = ((TextView) itemView.findViewById(R.id.tv_history_poker_one));
            mHistoryPokers[1] = ((TextView) itemView.findViewById(R.id.tv_history_poker_two));
            mHistoryPokers[2] = ((TextView) itemView.findViewById(R.id.tv_history_poker_three));

            mTvShape = ((TextView) itemView.findViewById(R.id.tv_shape));

            for (TextView historyPoker : mHistoryPokers) {
                historyPoker.setTypeface(MyApplication.getPokerTypeFace());
            }
        }
    }
}
